package de.htw.praxisprojekt;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

import de.htw.praxisprojekt.data.draw.DrawObject;
import de.htw.praxisprojekt.eps.EpsGraphics2D;

public class ExportUtils{

	public static final String EXT_EPS="eps";
	public static final String EXT_JPG="jpg";
	public static final String EXT_PNG="png";
	public static final String EXT_GIF="gif";

	public static void export(File file,String extension,LineAlgo lineAlgo){
		if(!file.getName().endsWith(extension))
			file=new File(file.getAbsolutePath()+"."+extension);
		if(extension.equals(EXT_EPS)){
			exportEPS(file,extension,lineAlgo);
		}else{
			exportImage(file,extension,lineAlgo);
		}
	}

	private static void exportImage(File file,String extension,LineAlgo lineAlgo){
		DrawPanel drawPanel=lineAlgo.getDrawPanel();
		BufferedImage image=new BufferedImage(drawPanel.getWidth(),drawPanel.getHeight(),BufferedImage.TYPE_INT_RGB);
		Graphics2D createGraphics=image.createGraphics();
		drawPanel.paint(createGraphics);
		try{
			ImageIO.write(image,extension,file);
		}catch(IOException e){
			e.printStackTrace();
		}
	}

	private static void exportEPS(File file,String extension,LineAlgo lineAlgo){
		DrawPanel drawPanel=lineAlgo.getDrawPanel();
		List<DrawObject> drawObjects=lineAlgo.getAlgoData().getDrawObjects();
		Graphics2D eps;
		try{
			eps=new EpsGraphics2D(lineAlgo.getLineAlgorithm().toString(),file,0,0,drawPanel.getWidth(),drawPanel.getHeight());
			eps.setClip(0,0,drawPanel.getWidth(),drawPanel.getHeight());
			for(int i=0;i<drawObjects.size();++i)
				drawObjects.get(i).draw(eps);
			System.out.println(eps.toString());
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}